package com.orangelife.service.property.model;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.orangelife.utils.StringUtils;

public class FeeMerge {

	public List<Map<String, Object>> dataList = new ArrayList<>();
	
	public List<Map<String, Object>> dataLists = new ArrayList<>();
	private String feeTypeID;
	private String feeTypeName;
	private String scode;
	private boolean isOne=false;
	public boolean ispre = false;
	public boolean hasMoney=true;
	public String price=null;
	public String feeAmount="";
	public int type=0;
	public List<Map<String, Object>> mergeList2() {
		int n = dataList.size();
		if (n == 2 && this.ispre) {
			// 合并预缴和
			Map<String, Object> p1 = dataList.get(0);
			Map<String, Object> p2 = dataList.get(1);

			BigDecimal r1 = new BigDecimal(StringUtils.toString(
					p1.get("feePrice"), "0"));
			BigDecimal r2 = new BigDecimal(StringUtils.toString(
					p2.get("feePrice"), "0"));

			if (p1.get("stardate") == null) {
				dataList.remove(0);

			} else {

				p1.put("enddate", p2.get("enddate"));
				p1.put("amount",
						new BigDecimal(StringUtils.toString(p1.get("amount"),
								"0")).add(new BigDecimal(StringUtils.toString(
								p2.get("amount"), "0"))));
				p1.put("feeAmount", p2.get("feeAmount"));
				p1.put("feePrice", p2.get("feePrice"));
				dataList.remove(1);

			}

			return dataList;

		} else {

			return dataList;
		}

	}
	public void merge2(){
		
		
		if(this.price!=null){
			this.isOne=true;
		}
				Map<String, Object> p=null;
			
				for (Map<String, Object> p1: dataList) {			
						 if(p==null){
							 p=	p1;
							 p.put("feePrice",StringUtils.toString(this.price));
							 if(type==0){
								 
								p.put("feeAmount", ""); 
							 }else{
								 if(type==1){

										p.put("feeAmount",this.feeAmount);		 
									 
								 }
								 if(type==2){
									 
									 if(!StringUtils.isBlank(p1.get("feeAmount"))){

											p.put("feeAmount", new BigDecimal(StringUtils.toString(p1.get("feeAmount"),
													"0")).setScale(2));									
										}
									  
									 
								 }
							
								 
								 
							 }
							  p.put("feeTypeName", this.feeTypeName);
							  
							  
						
								
							 
							 dataLists.add(p);
							 
							 
						 }else{
								p.put("amount", new BigDecimal(StringUtils.toString(p1.get("amount"),
										"0")).add(new BigDecimal(StringUtils.toString(
										p.get("amount"), "0"))));
								
								
//							 if(type==1){
//									if(!StringUtils.isBlank(StringUtils.toString(p1.get("feeAmount")))){
//										
//										p.put("feeAmount", StringUtils.toString(p1.get("feeAmount")));
//									}	
//									
//							 }
							 if(type==2){
							 if(!StringUtils.isBlank(p1.get("feeAmount"))){

									p.put("feeAmount", new BigDecimal(StringUtils.toString(p1.get("feeAmount"),
											"0")).add(new BigDecimal(StringUtils.toString(
											p.get("feeAmount"), "0"))).setScale(2));									
								}
						
								
							 }
							if(p.get("stardate")==null){
								p.put("stardate", p1.get("stardate").toString());
							}
							p.put("enddate", p1.get("enddate").toString()); 
							 
						 }
						 
						
						}
						
						
						
					
					
					
			
				
//				if(this.dataLists.size()==1&&new BigDecimal(StringUtils.toString(this.dataLists.get(0).get("amount"),"0")))
//			System.out.println(this.dataLists);	

			
			
			
		}
public void merge(){
	
	
if(type!=0){
	this.isOne=true;
}
		Map<String, Object> p=null;
		String tempPrice=null;
		boolean flag=true;
		for (Map<String, Object> p1: dataList) {
			
			if(!StringUtils.isBlank(StringUtils.toString(p1.get("feePrice")))){
				
				this.isOne=true;	
			}
		
			
			
			if(flag){
				
			
		
			
				
			  if(p==null){
				  
				  p=	p1;
				  if(type==3){
					  p.put("feePrice", this.price);
					  
				  }
				  
				  if(type!=0){
					  
					if(!StringUtils.isBlank(p1.get("feeAmount"))){
						
						p.put("feeAmount", new BigDecimal(p1.get("feeAmount").toString()).setScale(2));
						
						
					}  
					  
				  }
				  p.put("feeTypeName", this.feeTypeName);
				  
				 
				  dataLists.add(p); 
				  
					  
			  }
			  tempPrice=StringUtils.toString(p1.get("feePrice"),"");
			  if(BigDecimal.ZERO.compareTo(new BigDecimal(StringUtils.toString(p1.get("amount"), "0")))<0){

					p.put("enddate", p1.get("enddate"));
					p.put("amount",p1.get("amount"));
					p.put("stardate", p1.get("stardate"));
if(!StringUtils.isBlank(p1.get("feeAmount"))){
						
						p.put("feeAmount", new BigDecimal(p1.get("feeAmount").toString()).setScale(2));
						
						
					} 
p.put("feePrice", p1.get("feePrice"));
if(type==3){
	  p.put("feePrice", this.price);
	  
}
					
				
				  flag=false;
				}

			}else{
				if(type!=3&&!tempPrice.equals(StringUtils.toString(p1.get("feePrice"),""))){
					
					
					  p=	p1;
					  tempPrice=StringUtils.toString(p1.get("feePrice"),"");
					  p.put("feeTypeName", this.feeTypeName);
					  if(type!=0){
						  
							if(!StringUtils.isBlank(p1.get("feeAmount"))){
								
								p.put("feeAmount", new BigDecimal(p1.get("feeAmount").toString()).setScale(2));
							}  
							  
						  }
					  dataLists.add(p);						
					
				}else{
					p.put("amount", new BigDecimal(StringUtils.toString(p1.get("amount"),
								"0")).add(new BigDecimal(StringUtils.toString(
								p.get("amount"), "0"))));
					
					if(p.get("stardate")==null){
						p.put("stardate", p1.get("stardate").toString());
					}
					
					if(type==0){
						
						if(StringUtils.isBlank(StringUtils.toString(p.get("feeAmount")))){			
							p.put("feeAmount", StringUtils.toString(p1.get("feeAmount")));
						}
						
					}else{
						
						if(!StringUtils.isBlank(p1.get("feeAmount"))){
							
							
							p.put("feeAmount", new BigDecimal(StringUtils.toString(p1.get("feeAmount"),
									"0")).add(new BigDecimal(StringUtils.toString(
									p.get("feeAmount"), "0"))).setScale(2));
							
						}
					}
				if(type!=3){
					
					if(p.get("feePrice")==null){			
						p.put("feePrice", StringUtils.toString(p1.get("feePrice")));				
					}	
				}
				
					p.put("enddate", p1.get("enddate").toString());
				}
				
				
				
			}
			
			
		}
		
//		if(this.dataLists.size()==1&&new BigDecimal(StringUtils.toString(this.dataLists.get(0).get("amount"),"0")))
	System.out.println(this.dataLists);	

	
	
	
}
	public List<Map<String, Object>> mergeList() {

		int n = dataList.size();
		Map<String, Object> temp = null;
		if (n == 2) {
			Map<String, Object> p1 = dataList.get(0);
			Map<String, Object> p2 = dataList.get(1);

			if (p1.get("stardate") == null) {

				dataList.remove(0);

			} else {

				BigDecimal r1 = new BigDecimal(StringUtils.toString(
						p1.get("feePrice"), "0"));
				BigDecimal r2 = new BigDecimal(StringUtils.toString(
						p2.get("feePrice"), "0"));
				if (r1.compareTo(r2) == 0 || r1.compareTo(BigDecimal.ZERO) == 0) {

					p1.put("enddate", p2.get("enddate"));

					p1.put("amount",
							new BigDecimal(StringUtils.toString(
									p1.get("amount"), "0"))
									.add(new BigDecimal(StringUtils.toString(
											p2.get("amount"), "0"))));
					p1.put("feeAmount", p2.get("feeAmount"));
					p1.put("feePrice", p2.get("feePrice"));

					dataList.remove(1);

				}

			}

		} else if (n == 3 && this.ispre) {
			// 合并预缴和
			Map<String, Object> p1 = dataList.get(1);
			Map<String, Object> p2 = dataList.get(2);

			BigDecimal r1 = new BigDecimal(StringUtils.toString(
					p1.get("feePrice"), "0"));
			BigDecimal r2 = new BigDecimal(StringUtils.toString(
					p2.get("feePrice"), "0"));
			if (r1.compareTo(r2) == 0) {
				p1.put("enddate", p2.get("enddate"));
				p1.put("amount",
						new BigDecimal(StringUtils.toString(p1.get("amount"),
								"0")).add(new BigDecimal(StringUtils.toString(
								p2.get("amount"), "0"))));
				p1.put("feeAmount", p2.get("feeAmount"));
				p1.put("feePrice", p2.get("feePrice"));
				dataList.remove(2);

			}

		} else {

			return dataList;

		}

		return dataList;
	}

	public void init() {

		this.dataList.clear();
		this.ispre = false;
		this.setFeeTypeName(null);
		this.setFeeTypeID(null);

	}

	public String getFeeTypeID() {
		return feeTypeID;
	}

	public void setFeeTypeID(String feeTypeID) {
		this.feeTypeID = feeTypeID;
	}

	public String getFeeTypeName() {
		return feeTypeName;
	}

	public void setFeeTypeName(String feeTypeName) {
		this.feeTypeName = feeTypeName;
	}

	public String getScode() {
		return scode;
	}

	public void setScode(String scode) {
		this.scode = scode;
	}

	public boolean isOne() {
		return isOne;
	}

	public void setOne(boolean isOne) {
		this.isOne = isOne;
	}



}
